package com.taxigang.client.uibinder;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Session;
import com.taxigang.client.TaxiGangHandler;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.shared.ChitInfo;
import com.taxigang.shared.TaxiGangInfo;

public class Messages extends TaxiGangPanel {

	private static MessagesUiBinder uiBinder = GWT
	.create(MessagesUiBinder.class);

	interface MessagesUiBinder extends UiBinder<Widget, Messages> {
	}

	@UiField Button newMessageButton;
	@UiField Button closeButton;
	@UiField Button nextButton;
	@UiField Button previousButton;

	@UiField Label fromLabel;
	@UiField Label fromContentLabel;
	@UiField Label timeLabel;
	@UiField Label timeContentLabel;
	@UiField Label messageLabel;
	@UiField Label messageContentLabel;
	@UiField TextArea messageContentTextArea;
	@UiField Label mobileNumberLabel;
	@UiField Label mobileNumberContentLabel;
	@UiField Label headerLabel;


	public Messages() {
		initWidget(uiBinder.createAndBindUi(this));
		bind();
	}

	private void bind(){
		messageContentTextArea.setReadOnly(true);
		newMessageButton.setText(TaxiGangCreator.taxiGangConstants.newMessage());
		closeButton.setText(TaxiGangCreator.taxiGangConstants.close());
		mobileNumberLabel.setText(TaxiGangCreator.taxiGangConstants.mobileNumber());
		
		newMessageButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO store nickname
				taxiGangRoute.show(PanelType.NEW_MESSAGE);
			}
		});

		closeButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO store nickname
				taxiGangRoute.show(PanelType.MYTAXIGANGS);
			}
		});

		nextButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO store nickname
				index++;
				showContent();
			}
		});

		previousButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				index--;
				showContent();
			}
		});

	}

	private int index;
	public void show(){
		super.show();

		index = 0;

		showContent();
	}

	private void showContent(){

		TaxiGangInfo taxiGangInfo = Session.getInstance().getCurrentMyTaxiGang();
		ChitInfo[] chitList = taxiGangInfo.getChitList();
		if (index < chitList.length){
			ChitInfo chitInfo = chitList[index];


			int numChits = taxiGangInfo.getChitList().length;
			nextButton.setVisible(index < numChits-1);		
			previousButton.setVisible(index != 0);

			fromLabel.setText("From");
			fromContentLabel.setText(chitInfo.getAuthorName());
			timeLabel.setText("Author");
			timeContentLabel.setText(TaxiGangHandler.formatter.format(chitInfo.getDate()));
			messageLabel.setText("Text");
			String chitText = chitInfo.getChitText();
			messageContentTextArea.setText(chitText);
			mobileNumberContentLabel.setText(chitInfo.getMobileNumber());
			
			
			int displayIndex = index+1;
			headerLabel.setText(TaxiGangCreator.taxiGangConstants.messages()+" "+displayIndex+" of "+numChits);

		} else {
			throw new RuntimeException("Messages: out of bounds:index"+index+":chitlength"+chitList.length);
		}


	}

}
